/*
 * createTime: 2020/2/25 13:29
 * description: request请求封装
 */

import axios from 'axios';
import { baseUrl } from '../config/env';
import router from '@/router/index';
import { getStore, setStore, removeStore } from './store';

import Store from '@/store';

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.timeout = 60000;
// 添加请求拦截器
axios.interceptors.request.use(
	function(config) {
		if (config.url.search('login') === -1) {
			if (getStore('token') && getStore('token') !== 'undefined') {
				config.headers['Authorization'] = getStore('token');
			} else {
				// config.headers['Authorization'] = '';
			}
		} else {
			// config.headers['Authorization'] = '';
		}

		if (config.url.search('getYzfTokenByCode') >= 0) {
			config.headers['Authorization'] = '';
		}

		let reg = /(http|https):\/\/([\w.]+\/?)\S*/;

		if (reg.test(config.url)) {
			config.url = config.url + '?r=' + Date.parse(new Date());
		} else {
			config.url = baseUrl + config.url + '?r=' + Date.parse(new Date());
		}

		if (config.method == 'post') {
			if (!config.data) {
				config.data = {};
			}

			config.data.app = 'yzf';
			config.data.sdkType = 'yzf';
		} else {
			if (!config.params) {
				config.params = {};
			}

			config.params.app ? config.params.app : (config.params.app = 'yzf');
			config.params.sdkType = 'yzf';
		}

		return config;
	},
	function(error) {
		return Promise.reject(error);
	}
);

// 添加响应拦截器
axios.interceptors.response.use(
	function(response) {
		const { data } = response;

		// 重新执行授权流程
		if (response.status == 403) {
			if (!response.config.url.includes('selfmallLogin')) {
				setStore('token', '');
				Store.dispatch('LOGIN');
			}
		}

		switch (data.code) {
			case 1000:
				return data.data;
			default:
				return Promise.reject(data);
		}
	},
	function(error) {
		setStore('token', '');
		return Promise.reject(error);
	}
);

let cfg = {
	'/proxy-shop': 'www.baidu.com'
};
export class BaseService {
	request(params = {}) {
		if (this.prefix && !params.url_no_prefix) {
			params.url = '/' + this.prefix + params.url;
		}

		for (let el in cfg) {
			if (params.url.indexOf(el) === 0) {
				params.url = params.url.replace(el, cfg[el]);
			}
		}

		return axios({
			...params
		});
	}
	page(params = {}) {
		return this.request({
			url: '/page',
			params: {
				...params
			}
		});
	}
	list(params = {}) {
		return this.request({
			url: '/list',
			params: {
				...params
			}
		});
	}
	info(params = {}) {
		return this.request({
			url: '/info',
			params: {
				...params
			}
		});
	}
	add(params = {}) {
		return this.request({
			url: '/add',
			method: 'POST',
			data: {
				...params
			}
		});
	}
	update(params = {}) {
		return this.request({
			url: '/update',
			method: 'POST',
			data: {
				...params
			}
		});
	}
	remove(params = {}) {
		return this.request({
			url: '/remove',
			method: 'POST',
			data: {
				...params
			}
		});
	}
}

export function Service(value) {
	return function(target) {
		let prefix = '';
		if (value) {
			let type = typeof value;
			if (type === 'object') {
				prefix = value.prefix;
			} else {
				prefix = value;
			}
		}
		target.prototype.prefix = prefix;
	};
}
